package com.cmwa.solution.sys.msg.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.annotation.FormModel;
import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.sys.menu.entity.MenuVo;
import com.cmwa.solution.sys.menu.service.MenuService;
import com.cmwa.solution.sys.msg.service.IMsgConfService;
import com.cmwa.solution.sys.msg.service.IMsgEmpConfService;
import com.cmwa.solution.sys.msg.service.IMsgPublicEmailService;
import com.cmwa.solution.sys.msg.vo.MsgConfVo;
import com.cmwa.solution.sys.msg.vo.MsgEmpConfVo;
import com.cmwa.solution.sys.msg.vo.MsgPublicEmailVo;
import com.cmwa.solution.utils.SessionUtils;
import com.cmwa.solution.utils.cached.DictionaryCached;
import com.cmwa.solution.utils.cached.MsgBacklogCached;

@Controller
@RequestMapping("/msgconf")
public class MsgConfController extends BaseController{
	
	@Resource
	private IMsgConfService msgConfService;
	@Resource
	private IMsgEmpConfService msgEmpConfService;
	@Resource
	private IMsgPublicEmailService msgPublicEmailService;
	@Resource
	private MenuService menuService;
	
	@RequestMapping("/gotoCreate")
	public String gotoCreate(ModelMap model){
		return "sys/msg/createMsgConf";
	}
	
	@RequestMapping("/create")
	public String create(MsgConfVo msgConfVo,@FormModel("msgEmpConfVoR") MsgEmpConfVo msgEmpConfVoR,@FormModel("msgEmpConfVoC") MsgEmpConfVo msgEmpConfVoC,@RequestParam("publicEmailIdsR") String publicEmailIdsR,@RequestParam("publicEmailIdsC") String publicEmailIdsC){
		Employee epy=SessionUtils.getEmployee();
		
		msgConfVo.setCreateId(epy.getID());
		
		msgEmpConfVoR.setCreateId(epy.getID());
		
		msgEmpConfVoC.setCreateId(epy.getID());
		
		int rst=msgConfService.create(msgConfVo);
		
		msgEmpConfVoR.setMsgConfId(msgConfVo.getId());
		msgEmpConfVoC.setMsgConfId(msgConfVo.getId());
		
		int rst2=msgEmpConfService.insertConfEmpR(msgEmpConfVoR);
		int rst3=msgEmpConfService.insertConfEmpC(msgEmpConfVoC);
		
		if(publicEmailIdsR!=null && !"".equalsIgnoreCase(publicEmailIdsR.trim())){
			Map<String, String> params_pub_r=new HashMap<String, String>();
			params_pub_r.put("CONF_ID", msgConfVo.getId());
			params_pub_r.put("EMAIL_TYPE", "R");
			msgPublicEmailService.deletePublicEmailByConfId(params_pub_r);
			msgPublicEmailService.insertPublicEmailByConfId(msgConfVo.getId(), "R", StringUtils.split(publicEmailIdsR, ","));
		}
		
		if(publicEmailIdsC!=null && !"".equalsIgnoreCase(publicEmailIdsC.trim())){
			Map<String, String> params_pub_c=new HashMap<String, String>();
			params_pub_c.put("CONF_ID", msgConfVo.getId());
			params_pub_c.put("EMAIL_TYPE", "C");
			msgPublicEmailService.deletePublicEmailByConfId(params_pub_c);
			msgPublicEmailService.insertPublicEmailByConfId(msgConfVo.getId(), "C", StringUtils.split(publicEmailIdsC, ","));
		}
		
		if(rst>0 && rst2>0 && rst3>0){
			return redirectSuccess();
		}else{
			return "sys/msg/fail";
		}
	}
	
	
	@RequestMapping("/msgConfDetailView")
	public String msgConfDetailView(@RequestParam("msgConfId") String msgConfId,ModelMap model){
		
		MsgConfVo msgConfVo=msgConfService.getMsgConf(msgConfId);
		msgConfVo.sendTypeSplit();
		model.addAttribute("msgConf", msgConfVo);
		
		MsgEmpConfVo msgEmpConfVoR = msgEmpConfService.getMsgEmpConfR(msgConfVo.getId());
		if(msgEmpConfVoR!=null){
			msgEmpConfVoR.specialEmpsSplit();
			
			Map<String, String> m=msgEmpConfService.getMsgEmpConfNameIdByIds(StringUtils.split(msgEmpConfVoR.getEmpIds(), ","));
			
			msgEmpConfVoR.setEmpIds(m.get("empIds"));
			msgEmpConfVoR.setEmpIdsNM(m.get("empNames"));
			
		}
		// 收件人
		model.addAttribute("msgEmpConfR", msgEmpConfVoR);
		
		MsgEmpConfVo msgEmpConfVoC=msgEmpConfService.getMsgEmpConfC(msgConfVo.getId());
		if(msgEmpConfVoC!=null){
			msgEmpConfVoC.specialEmpsSplit();
			
			Map<String, String> m=msgEmpConfService.getMsgEmpConfNameIdByIds(StringUtils.split(msgEmpConfVoC.getEmpIds(), ","));
			
			msgEmpConfVoC.setEmpIds(m.get("empIds"));
			msgEmpConfVoC.setEmpIdsNM(m.get("empNames"));
			
		}
		// 抄送人
		model.addAttribute("msgEmpConfC", msgEmpConfVoC);
		
		Map<String, String> params_pub_r=new HashMap<String, String>();
		params_pub_r.put("CONF_ID", msgConfId);
		params_pub_r.put("EMAIL_TYPE", "R");
		List<MsgPublicEmailVo> publicEmailsR=msgPublicEmailService.queryPublicEmailByConfId(params_pub_r);
		String pubNamesR="";
		String pubIdsR="";
		if(publicEmailsR!=null && publicEmailsR.size()>0){
			for(MsgPublicEmailVo one : publicEmailsR){
				pubNamesR=pubNamesR+one.getEmailName()+",";
				pubIdsR=pubIdsR+one.getId()+",";
			}
			pubNamesR=pubNamesR.substring(0, pubNamesR.length()-1);
			pubIdsR=pubIdsR.substring(0, pubIdsR.length()-1);
		}
		model.addAttribute("publicEmailNamesR", pubNamesR);
		model.addAttribute("publicEmailIdsR", pubIdsR);
		
		Map<String, String> params_pub_c=new HashMap<String, String>();
		params_pub_c.put("CONF_ID", msgConfId);
		params_pub_c.put("EMAIL_TYPE", "C");
		List<MsgPublicEmailVo> publicEmailsC=msgPublicEmailService.queryPublicEmailByConfId(params_pub_c);
		String pubNamesC="";
		String pubIdsC="";
		if(publicEmailsC!=null && publicEmailsC.size()>0){
			for(MsgPublicEmailVo one : publicEmailsC){
				pubNamesC=pubNamesC+one.getEmailName()+",";
				pubIdsC=pubIdsC+one.getId()+",";
			}
			pubNamesC=pubNamesC.substring(0, pubNamesC.length()-1);
			pubIdsC=pubIdsC.substring(0, pubIdsC.length()-1);
		}
		model.addAttribute("publicEmailNamesC", pubNamesC);
		model.addAttribute("publicEmailIdsC", pubIdsC);
		return "system/sys/msg/msgConfDetail";
	}
	
	

	
	/**
	 * 修改消息配置页面
	 * @author ex-hejm
	 * @date 2018年10月23日 上午11:14:37
	 * @param msgConfId
	 * @param model
	 * @return
	 */
	@RequestMapping("/msgConfUpdateView")
	public String msgConfUpdateView(@RequestParam("msgConfId") String msgConfId,ModelMap model){
		List<MenuVo> rootMenu = new ArrayList<MenuVo>();//menuService.queryThreeLevelMenuList();
		model.put("rootMenu", rootMenu);
		
		MsgConfVo msgConfVo = msgConfService.getMsgConf(msgConfId);
		msgConfVo.sendTypeSplit();
		model.addAttribute("msgConf", msgConfVo);
		
		MsgEmpConfVo msgEmpConfVoR = msgEmpConfService.getMsgEmpConfR(msgConfVo.getId());
		if(msgEmpConfVoR!=null){
			msgEmpConfVoR.specialEmpsSplit();
			
			Map<String, String> m=msgEmpConfService.getMsgEmpConfNameIdByIds(StringUtils.split(msgEmpConfVoR.getEmpIds(), ","));
			
			msgEmpConfVoR.setEmpIds(m.get("empIds"));
			msgEmpConfVoR.setEmpIdsNM(m.get("empNames"));
			
		}
		model.addAttribute("msgEmpConfR", msgEmpConfVoR);
		
		MsgEmpConfVo msgEmpConfVoC=msgEmpConfService.getMsgEmpConfC(msgConfVo.getId());
		if(msgEmpConfVoC!=null){
			msgEmpConfVoC.specialEmpsSplit();
			
			Map<String, String> m=msgEmpConfService.getMsgEmpConfNameIdByIds(StringUtils.split(msgEmpConfVoC.getEmpIds(), ","));
			
			msgEmpConfVoC.setEmpIds(m.get("empIds"));
			msgEmpConfVoC.setEmpIdsNM(m.get("empNames"));
			
		}
		model.addAttribute("msgEmpConfC", msgEmpConfVoC);
		
		Map<String, String> params_pub_r=new HashMap<String, String>();
		params_pub_r.put("CONF_ID", msgConfId);
		params_pub_r.put("EMAIL_TYPE", "R");
		List<MsgPublicEmailVo> publicEmailsR=msgPublicEmailService.queryPublicEmailByConfId(params_pub_r);
		String pubNamesR="";
		String pubIdsR="";
		if(publicEmailsR!=null && publicEmailsR.size()>0){
			for(MsgPublicEmailVo one : publicEmailsR){
				pubNamesR=pubNamesR+one.getEmailName()+",";
				pubIdsR=pubIdsR+one.getId()+",";
			}
			pubNamesR=pubNamesR.substring(0, pubNamesR.length()-1);
			pubIdsR=pubIdsR.substring(0, pubIdsR.length()-1);
		}
		model.addAttribute("publicEmailNamesR", pubNamesR);
		model.addAttribute("publicEmailIdsR", pubIdsR);
		
		Map<String, String> params_pub_c=new HashMap<String, String>();
		params_pub_c.put("CONF_ID", msgConfId);
		params_pub_c.put("EMAIL_TYPE", "C");
		List<MsgPublicEmailVo> publicEmailsC=msgPublicEmailService.queryPublicEmailByConfId(params_pub_c);
		String pubNamesC="";
		String pubIdsC="";
		if(publicEmailsC!=null && publicEmailsC.size()>0){
			for(MsgPublicEmailVo one : publicEmailsC){
				pubNamesC=pubNamesC+one.getEmailName()+",";
				pubIdsC=pubIdsC+one.getId()+",";
			}
			pubNamesC=pubNamesC.substring(0, pubNamesC.length()-1);
			pubIdsC=pubIdsC.substring(0, pubIdsC.length()-1);
		}
		model.addAttribute("publicEmailNamesC", pubNamesC);
		model.addAttribute("publicEmailIdsC", pubIdsC);
		
		return "system/sys/msg/msgConfUpdate";
	}
	
	@RequestMapping("/updateMsgConf")
	public String updateMsgConf(MsgConfVo msgConfVo,@FormModel("msgEmpConfVoR") MsgEmpConfVo msgEmpConfVoR,@FormModel("msgEmpConfVoC") MsgEmpConfVo msgEmpConfVoC,@RequestParam("publicEmailIdsR") String publicEmailIdsR,@RequestParam("publicEmailIdsC") String publicEmailIdsC){
		Employee epy=SessionUtils.getEmployee();
		
		msgConfVo.setModifyId(epy.getID());
		
		msgEmpConfVoR.setCreateId(epy.getID());
		
		msgEmpConfVoC.setCreateId(epy.getID());
		
		int rst=msgConfService.update(msgConfVo);
		
		msgEmpConfService.deleteMsgEmpConfR(msgConfVo.getId());
		
		msgEmpConfVoR.setMsgConfId(msgConfVo.getId());
		
		int rst2=msgEmpConfService.insertConfEmpR(msgEmpConfVoR);
		
		msgEmpConfService.deleteMsgEmpConfC(msgConfVo.getId());
		
		msgEmpConfVoC.setMsgConfId(msgConfVo.getId());
		
		int rst3=msgEmpConfService.insertConfEmpC(msgEmpConfVoC);
		
		
		Map<String, String> params_pub_r=new HashMap<String, String>();
		params_pub_r.put("CONF_ID", msgConfVo.getId());
		params_pub_r.put("EMAIL_TYPE", "R");
		msgPublicEmailService.deletePublicEmailByConfId(params_pub_r);
		
		if(publicEmailIdsR!=null && !"".equalsIgnoreCase(publicEmailIdsR.trim())){
			msgPublicEmailService.insertPublicEmailByConfId(msgConfVo.getId(), "R", StringUtils.split(publicEmailIdsR, ","));
		}
		
		
		Map<String, String> params_pub_c=new HashMap<String, String>();
		params_pub_c.put("CONF_ID", msgConfVo.getId());
		params_pub_c.put("EMAIL_TYPE", "C");
		msgPublicEmailService.deletePublicEmailByConfId(params_pub_c);
		
		if(publicEmailIdsC!=null && !"".equalsIgnoreCase(publicEmailIdsC.trim())){
			msgPublicEmailService.insertPublicEmailByConfId(msgConfVo.getId(), "C", StringUtils.split(publicEmailIdsC, ","));
		}
		
		
		if(rst>0 && rst2>0 && rst3>0){
			return redirectSuccess();
		}else{
			return "sys/msg/fail";
		}
	}
	
	
	
	@RequestMapping("/gotoDelete")
	public String gotoDelete(@RequestParam("msgConfId") String msgConfId,ModelMap model){
		int rst=msgConfService.deleteMsgConf(msgConfId);
		
		
		if(rst>0){
			return redirectSuccess();
		}else{
			return "sys/msg/fail";
		}
	}
	
	/**
	 * 消息配置列表页面
	 * @author ex-hejm
	 * @date 2018年10月23日 下午1:20:46
	 * @param model
	 * @return
	 */
	@RequestMapping("/msgConfListView")
	public String gotoListView(ModelMap model){
		List<DictionaryVo> msgSendTypeList = DictionaryCached.getDictionaryByType("MSG_SEND_TYPE");
		model.addAttribute("msgSendTypeList", msgSendTypeList);
		return "system/sys/msg/msgConfList";
	}
	
	@RequestMapping("/msgConfList")
	@ResponseBody
	public Page<MsgConfVo> msgConfList(SearchParam sp){
		Page<MsgConfVo> msgConfVos = msgConfService.msgConfListPage(sp);
		return msgConfVos;
	}
	
	@RequestMapping("/gotoStart")
	public String gotoStart(@RequestParam("msgConfId") String msgConfId){
		int rst=msgConfService.startConf(msgConfId);
		
		
		if(rst>0){
			return redirectSuccess();
		}else{
			return "sys/msg/fail";
		}
	}
	
	@RequestMapping("/gotoStop")
	public String gotoStop(@RequestParam("msgConfId") String msgConfId){
		int rst=msgConfService.stopConf(msgConfId);
		
		
		if(rst>0){
			return redirectSuccess();
		}else{
			return "sys/msg/fail";
		}
	}
	
	/**
	 * 更新待办缓存
	 * @param sp
	 * @return
	 */
	@RequestMapping("/refreshMsgEmpConfRs")
	public String refreshMsgEmpConfRs(SearchParam sp) {
		MsgBacklogCached.msgConfVoEmpRs.clear();
		sp.getSp().put("sendTypes", "BM");
		sp.getSp().put("runStatus", "R");
		List<MsgConfVo> msgConfVos = msgConfService.msgConfList(sp);
		for (int i = 0; i < msgConfVos.size(); i++) {
			MsgConfVo msgConfVo = msgConfVos.get(i);
			if (msgConfVo.getSendType().equals("Backlog")) {
				MsgBacklogCached.msgConfVoEmpRs.add(msgConfVo);
			}
		}
		return redirectSuccess();
	}
	
	/**
	 * 新增主消息配置页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/msgConfAddView")
	public String msgConfAddView(ModelMap model){
		//List<MenuVo> rootMenu = menuService.queryThreeLevelMenuList();
		List<MenuVo> rootMenu = new ArrayList<MenuVo>();
		model.put("rootMenu", rootMenu);
		
		String sortNo = msgConfService.getSortNoBySendType("Notice");
		model.addAttribute("sortNo", sortNo);
		
		return "system/sys/msg/msgConfAdd";
	}
	
	/**
	 * 根据sendType获取sortNo
	 * @author ex-hejm
	 * @date 2018年10月23日 上午10:33:51
	 * @param sendType
	 * @return
	 */
	@RequestMapping("/getSortNoBySendType")
	@ResponseBody
	public String getSortNoBySendType(@RequestParam("sendType") String sendType) {
		return msgConfService.getSortNoBySendType(sendType);
	}

	/**
	 * 查询msgCode是否已存在
	 * @author ex-hejm
	 * @date 2018年10月23日 下午2:53:12
	 * @param msgCode
	 * @param oldMsgCode
	 * @return
	 */
	@RequestMapping("/validateMsgCodeIsExist")
	@ResponseBody
	public Result<Object> validateMsgCodeIsExist(@RequestParam("msgCode") String msgCode, @RequestParam(value = "oldMsgCode", defaultValue = "") String oldMsgCode) {
		return msgConfService.validateMsgCodeIsExist(msgCode, oldMsgCode);
	}
	
	@RequestMapping("/addMsgConf")
	public String addMsgConf(
			MsgConfVo msgConfVo, 
			@FormModel("msgEmpConfVoR") MsgEmpConfVo msgEmpConfVoR,
			@FormModel("msgEmpConfVoC") MsgEmpConfVo msgEmpConfVoC,
			@RequestParam("publicEmailIdsR") String publicEmailIdsR,
			@RequestParam("publicEmailIdsC") String publicEmailIdsC){
		
		Employee epy = SessionUtils.getEmployee();
		msgConfVo.setCreateId(epy.getID());
		msgEmpConfVoR.setCreateId(epy.getID());
		msgEmpConfVoC.setCreateId(epy.getID());
		
		msgConfService.insertMsgConf(msgConfVo);
		
		msgEmpConfVoR.setMsgConfId(msgConfVo.getId());
		msgEmpConfVoC.setMsgConfId(msgConfVo.getId());
		
		int rst2 = msgEmpConfService.insertConfEmpR(msgEmpConfVoR);
		int rst3 = msgEmpConfService.insertConfEmpC(msgEmpConfVoC);
		
		if(publicEmailIdsR!=null && !"".equalsIgnoreCase(publicEmailIdsR.trim())){
			Map<String, String> params_pub_r=new HashMap<String, String>();
			params_pub_r.put("CONF_ID", msgConfVo.getId());
			params_pub_r.put("EMAIL_TYPE", "R");
			msgPublicEmailService.deletePublicEmailByConfId(params_pub_r);
			msgPublicEmailService.insertPublicEmailByConfId(msgConfVo.getId(), "R", StringUtils.split(publicEmailIdsR, ","));
		}
		
		if(publicEmailIdsC!=null && !"".equalsIgnoreCase(publicEmailIdsC.trim())){
			Map<String, String> params_pub_c=new HashMap<String, String>();
			params_pub_c.put("CONF_ID", msgConfVo.getId());
			params_pub_c.put("EMAIL_TYPE", "C");
			msgPublicEmailService.deletePublicEmailByConfId(params_pub_c);
			msgPublicEmailService.insertPublicEmailByConfId(msgConfVo.getId(), "C", StringUtils.split(publicEmailIdsC, ","));
		}
		
		if(rst2>0 && rst3>0){
			return redirectSuccess();
		}else{
			return "sys/msg/fail";
		}
	}
}